package com.yanqu.road.server.manger.activity.recycle;

import com.yanqu.road.entity.activity.ActivityItemRecycleData;
import com.yanqu.road.entity.activity.summergames.SummerGamesConfig;
import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eGoodsType;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.enums.eMailType;
import com.yanqu.road.entity.player.UserBagItem;
import com.yanqu.road.entity.player.UserInfo;
import com.yanqu.road.logic.bussiness.player.UserBagBussiness;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.BagModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.logic.mail.MailManager;
import com.yanqu.road.server.manger.MultipleLanguageMgr;
import com.yanqu.road.server.manger.activity.summergames.SummerGamesActivityMgr;
import com.yanqu.road.server.manger.config.GoodsMgr;
import com.yanqu.road.server.manger.log.LogMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.property.PropertyHelper;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SummerGamesRecycleStrategy extends AbstractItemRecycleStrategy {

    @Override
    public void markNeedRecycleItem() {
        SummerGamesConfig config = SummerGamesActivityMgr.getSummerGamesConfig();
        if (config != null) {
            List<String> paramList = new ArrayList<>();
            long recycleTime = config.getActivityInfo().getEndShowTime() * 1000L + 600 * 1000;
            paramList.add(String.valueOf(config.getItemId()));
            paramList.add(String.valueOf(config.getEXCHANGE_SCORE_ITEM_ID()));
            paramList.add("1");
            paramList.add(config.getActivityInfo().getActivityName());
            paramList.add(config.getSUMMERGAME_MEDALS_RECLAIM());
            ActivityItemRecycleMgr.addRecycleItemData(config.getItemId(), activityType, 1, recycleTime, paramList);
        }
    }

    @Override
    protected void initActivityType() {
        activityType = eActivityType.SummerGames.getValue();
    }

    @Override
    protected void doRecycle(long userId, ActivityItemRecycleData data) {
//        item2Score(userId, data);
        UserBagItem item = getUserBagItem(userId, data.getItemId());
        if (item != null && item.getNum() > 0) {
            removeItem(item);
        }
        try {
//            clearUserBagItems(userId, eGoodsType.SummerGamesMedals.getValue());
            if(data.getParamList().size() > 4){
                Map<Integer, String> quelityRewardMap = new HashMap<>();
                String reclaim = data.getParamList().get(4);
                List<String> medalsReclaimList = StringUtils.stringToStringList(reclaim, "\\|");
                for (String paramItem : medalsReclaimList) {
                    List<String> itemList = StringUtils.stringToStringList(paramItem, ",");
                    quelityRewardMap.put(Integer.parseInt(itemList.get(0)), itemList.get(1));
                }

                GamePlayer player = GamePlayerMgr.getOnlinePlayer(userId);
                Map<Integer, UserBagItem> userBagItemMap;
                if (player != null) {
                    userBagItemMap =  player.getModule(BagModule.class).getUserBagItemMap();
                }else {
                    userBagItemMap = UserBagBussiness.getUserBagItemMap(userId);
                }
                if(userBagItemMap == null || userBagItemMap.isEmpty()){
                    return;
                }
                List<UserBagItem> itemList = new ArrayList<>();
                for (Map.Entry<Integer, UserBagItem> entry : userBagItemMap.entrySet()) {
                    UserBagItem bagItem = entry.getValue();
                    if(bagItem.getNum() <= 0){
                        continue;
                    }
                    GoodsInfo goodsInfo = GoodsMgr.getGoodsById(bagItem.getGoodsId());
                    if(goodsInfo == null || goodsInfo.getType() != eGoodsType.SummerGamesMedals.getValue()){
                        continue;
                    }
                    itemList.add(bagItem);
                }
                Property rebackReward = new Property();
                for (UserBagItem bagItem : itemList) {
                    long num = bagItem.getNum();
                    player = GamePlayerMgr.getOnlinePlayer(userId);
                    log.info("userId : {} 回收道具 {}， 数量： {}", userId, bagItem.getGoodsId(),num);
                    GoodsInfo goodsInfo = GoodsMgr.getGoodsById(bagItem.getGoodsId());
                    if(goodsInfo != null){
                        String propertyStr = quelityRewardMap.get(goodsInfo.getQuality());
                        if(!StringUtils.isNullOrEmpty(propertyStr)){
                            Property property = PropertyHelper.parseStringToProperty(propertyStr);
                            property.rideProperty(num);
                            rebackReward.addProperty(property);
                        }
                    }
                    if (player != null) {
                        player.getModule(CurrencyModule.class).removeCurrency(new Property(bagItem.getGoodsId(), BigInteger.valueOf(bagItem.getNum())), eLogMoneyType.Activity, eLogMoneyType.ActivityItemRecycle);
                    } else {
                        bagItem.setNum(0);
                        boolean success = UserBagBussiness.updateUserBagItem(bagItem);
                        if (success) {
                            long vipExp = 0;
                            UserInfo userInfo = UserMgr.getUserInfo(userId);
                            if (userInfo != null) {
                                vipExp = userInfo.getVipExp();
                            }
                            LogMgr.addLogGoods(bagItem.getGoodsId(), userId, false, num, 0, eLogMoneyType.ActivityItemRecycle, vipExp);
                        }
                    }
                }
                if(!rebackReward.isNothing()){
                    String language = UserMgr.getUserInfo(userId).getLanguage();
                    String mailTitle = MultipleLanguageMgr.getContent(MailManager.SUMMER_GAMES_MAIL_TITLE_7, language);
                    String mailContent = MultipleLanguageMgr.getContent(MailManager.SUMMER_GAMES_CONTENT_7, language);
                    MailManager.sendMail(userId, eMailType.SYSTEM.getValue(), PropertyHelper.parsePropertyToString(rebackReward), mailContent, mailTitle);
                }
            }
        }catch (Exception e){
            log.error("", e);
        }
    }

    @Override
    public boolean isActivityRunning() {
        return false;
    }
}
